package com.leyou.gateway.filters;

import com.leyou.auth.utils.JwtUtils;
import com.leyou.common.utils.CookieUtils;
import com.leyou.gateway.config.FilterProperties;
import com.leyou.gateway.config.JwtProperties;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Component
@EnableConfigurationProperties({JwtProperties.class, FilterProperties.class})
public class LoginFilter extends ZuulFilter {

    @Autowired
    private JwtProperties jwtProps;

    @Autowired
    private FilterProperties filterProps;

    /**
     * filter type 四种
     * <p>
     * <p>
     * pre,routing,post,error
     * type=pre
     * <p>
     * pre--->routing---->post
     * <p>
     * pre--->error->post
     * <p>
     * pre---->routing----->error-->post
     * <p>
     * pre---->routing--->post---->error
     *
     * @return
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * 优先级
     *
     * @return
     */
    @Override
    public int filterOrder() {
        return 1;
    }


    /**
     * 决定是否启用过滤器，
     * <p>
     * true 表示启用
     * <p>
     * false 不起用
     *
     * @return
     */
    @Override
    public boolean shouldFilter() {

        //获取当前请求上下文
        RequestContext currentContext = RequestContext.getCurrentContext();

        //获取当前的请求对象
        HttpServletRequest request = currentContext.getRequest();

        //获取当前请求的资源的描述信息
        String requestURI = request.getRequestURI();//api/auth/accredit

        //获取所有的要放行的路径
        List<String> allowPaths = filterProps.getAllowPaths();//api/auth

        //遍历要放行的路径，是否请求资源的路径以我们配置的某个路径开始的，如果是，则布兰姐
        for (String allowPath : allowPaths) {
            if (requestURI.startsWith(allowPath)){
               return false;//布兰姐
            }
        }
        return true;
    }

    /**
     * 真正的过滤拦截逻辑
     * <p>
     * 放行不用管
     * <p>
     * 拦截就是不响应，所以要把response，设置为false
     * 并且返回一个状态码401
     *
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        //解析一下token，如果能解析，则放行，不能解析则拦截不响应

        //获取当前请求上下文
        RequestContext currentContext = RequestContext.getCurrentContext();

        //获取当前的请求对象
        HttpServletRequest request = currentContext.getRequest();

        //使用工具类获取cookie
        String token = CookieUtils.getCookieValue(request, jwtProps.getCookieName());

        //判空
        if (null != token) {//不为空
            try {
                JwtUtils.getInfoFromToken(token, jwtProps.getPublicKey());
            } catch (Exception e) {
                e.printStackTrace();

                currentContext.setSendZuulResponse(false);
                currentContext.setResponseStatusCode(401);
            }
        } else {//token为空，

            currentContext.setSendZuulResponse(false);
            currentContext.setResponseStatusCode(401);

        }

        return null;
    }
}
